<template>
    <el-container class="applet">
        <el-main class="wrapper">
            <section class="panel cms-content">
                <div class="panel-body">
                    <div class="btn-group" style="margin-bottom: 15px">
                        <el-button @click="list" type="primary" icon="fa fa-refresh"
                                   size="small"> 刷新
                        </el-button>
                        <el-tooltip class="item" effect="dark" content="如果提示access_token错误，请点击该按钮" placement="bottom">
                            <el-button @click="retoken" type="primary" icon="fa fa-refresh"
                                       size="small"> 重置
                            </el-button>
                        </el-tooltip>
                        <el-badge :value="number" class="item" style="margin:0 15px">
                            <el-button size="small">有效推送</el-button>
                        </el-badge>
                    </div>
                    <el-table v-loading="loading"
                              element-loading-text="努力加载中..."
                              element-loading-spinner="el-icon-loading"
                              :data="tableData" :border="true" style="width: 100%">
                        <el-table-column prop="title" label="标题" width="200px"></el-table-column>
                        <el-table-column prop="template_id" label="模板ID" width="500px"></el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="primary" size="mini" @click="edit(scope.row)" icon="fa fa-edit" plain>
                                    编辑
                                </el-button>
                                <el-button icon="fa fa-send"
                                           @click="push(scope.row)" type="primary" size="mini" plain> 推送
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </section>
        </el-main>
        <el-dialog
                v-if="dialog.show"
                :title="dialog.title"
                custom-class="cms-dialog"
                :top="dialog.top"
                :visible.sync="dialog.show"
                :width="dialog.width">
            <el-form :model="dataForm" :rules="rules" ref="dataForm" label-width="120px" class="demo-ruleForm">
                <el-form-item label="模板ID：" prop="template_id">
                    <el-input v-model="dataForm.template_id" disabled placeholder="请填写模板ID"></el-input>
                </el-form-item>
                <el-form-item label="跳转页：" prop="page">
                    <el-input v-model="dataForm.page" placeholder="请填写指定跳转页"></el-input>
                </el-form-item>
                <el-form-item
                        v-for="(item, index) in tmpDialogName"
                        :label="item"
                        :key="item.key"
                        :rules="{required: true, message: item+'不能为空', trigger: 'blur'}"
                >
                    <el-input v-model="dataForm.keywords[0][index]" :placeholder="item+'不能为空'"></el-input>
                </el-form-item>
                <el-form-item label="放大关键字">
                    <el-select v-model="dataForm.emphasis">
                        <el-option key="" label="无" value=""></el-option>
                        <el-option v-for="(item1, index1) in tmpDialogName" :key="index1" :label="item1"
                                   :value="index1"></el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialog.show = false">取 消</el-button>
                <el-button type="primary" @click="update('dataForm')">确 定</el-button>
              </span>
        </el-dialog>
    </el-container>
</template>
<script>
    import {mapGetters} from 'vuex';
    import {MessageList, MessageUpdate, MessagePush, reToken} from '../../utils/request';

    export default {
        data() {
            return {
                tableData: [],//列表数据
                tmpDialog: false,
                tmpDialogTitle: '',
                tmpDialogName: [],
                tmpDialogKey: [],
                applet: 'apang',
                dialog: {
                    title: '',
                    top: '10vh',
                    show: false,
                    width: '550px'
                },
                dataForm:null,
                rules: {
                    page: [{required: true, message: '请指定跳转页', trigger: 'blur'}],
                },
                number: 0,
            }
        },
        activated() {
            this.$store.dispatch('setAPangActive', '/apang/message');
            this.list();
        },
        computed: {
            ...mapGetters(['loading'])
        },
        methods: {
            //获取列表
            list() {
                this.$store.dispatch('setLoading', true);
                MessageList({params: {applet: this.applet}}).then((response) => {
                    if (response.code) {
                        this.$message.error(response.msg);
                    } else {
                        this.$store.dispatch('setLoading', false);
                        this.tableData = response.data.list;
                        this.number = response.data.number;
                    }
                })
            },
            //推送
            push(info) {
                this.$store.dispatch('setLoading', true);
                this.$confirm('确定要推送该模板消息吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    MessagePush({id: info.template_id, applet: this.applet}).then((response) => {
                        this.$store.dispatch('setLoading', false);
                        if (response.code) {
                            this.$message.error(response.msg);
                        } else {
                            this.$message({
                                type: 'success',
                                duration: '1000',
                                message: response.msg,
                            });
                        }
                    });
                }).catch(() => {
                    this.$store.dispatch('setLoading', false);
                });
            },
            //编辑
            edit(info) {
                this.dialog.title = '编辑模板消息';
                this.dialog.show = true;
                this.tmpDialogName = info.keyword_text_arr;
                this.tmpDialogKey = info.keywords;
                this.dataForm = {
                    title: info.title,
                    template_id: info.template_id,
                    page: info.page,
                    keywords: info.form,
                    emphasis: info.emphasis
                };
            },
            //更新
            update(formName) {
                let that = this;
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.dataForm.applet = that.paramsName
                        MessageUpdate(this.dataForm).then((response) => {
                            if (response.code) {
                                this.$message.error(response.msg);
                            } else {
                                this.tmpDialog = false;
                                this.$message({
                                    type: 'success',
                                    duration: '1000',
                                    message: response.msg,
                                    onClose: () => {
                                        this.list();
                                    }
                                });
                            }
                        });
                    }
                });
            },
            // 重置token
            retoken() {
                this.$store.dispatch('setLoading', true);
                this.$confirm('确定要重置token吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    reToken({applet:this.applet}).then((response) => {
                        this.$store.dispatch('setLoading', false);
                        if (response.code) {
                            this.$message.error(response.msg);
                        } else {
                            this.$message.success(response.msg);
                        }
                    })
                }).catch(() => {
                    this.$store.dispatch('setLoading', false);
                });
            },
        },
    }
</script>
